<template>
  <div class="app-container calendar-list-container">
    <!--查询-->
    <div class="filter-container">
      <el-input style="width:200px;" v-model="tableQuery.rname" placeholder="角色名"></el-input>
      <span style="margin-right: 15px;"></span>
      <el-tooltip class="item" content="搜索" placement="top">
        <el-button icon="el-icon-search" circle @click="fetchData()"></el-button>
      </el-tooltip>
    </div>
    <div style="margin-bottom: 30px;"></div>
    <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleCreate">{{textMap.create}}</el-button>
    <div style="margin-bottom: 30px;"></div>
    <!--列表-->
    <el-table style="width: 100%"
              :data="tableData"
              v-loading.body="tableLoading"
              element-loading-text="Loading"
              border fit highlight-current-row>
      <el-table-column align='center' width='100px' prop="rid" label="角色id" sortable></el-table-column>
      <el-table-column align='center' width='200px' prop="rname" label="角色名" ></el-table-column>
      <el-table-column align='center' width='200px' prop="rdesc" label="角色描述" ></el-table-column>
      <el-table-column align='center' width='150px' prop="rval" label="角色值" ></el-table-column>
      <el-table-column align='center' width='160px' prop="created" label="创建时间" ></el-table-column>
      <el-table-column align='center' width='160px' prop="updated" label="更新时间" ></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-tooltip content="编辑" placement="top">
            <el-button @click="handleUpdate(scope.$index,scope.row)" size="medium" type="info" icon="el-icon-edit" circle plain></el-button>
          </el-tooltip>
          <el-tooltip content="修改权限" placement="top" v-if="!hasAdminRole(scope.row)">
            <el-button @click="handleUpdateRolePerms(scope.$index,scope.row)" size="medium" type="warning" icon="el-icon-view" circle plain></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top" v-if="!hasAdminRole(scope.row)">
            <el-button @click="handleDelete(scope.$index,scope.row)" size="medium" type="danger" icon="el-icon-delete" circle plain></el-button>
          </el-tooltip>
          <el-popover trigger="hover" placement="top" v-else style="display: inline-block;">
            <el-alert type="warning" :closable="false" title="权限说明">
              <div>为保证管理员在系统中的最高权限</div>
              <div>不允许编辑管理员自身的权限</div>
              <div>不允许删除管理员角色</div>
            </el-alert>
            <div slot="reference" >
              <el-tag style="margin-left: 10px;" type="info">权限说明</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-bottom: 30px;"></div>
    <!-- 分页 -->
    <div class='pagination-container'>
      <el-pagination background @size-change='handleSizeChange' @current-change='handleCurrentChange' :current-page='tableQuery.page'
        :page-sizes='[10,20,30,50]' :page-size='tableQuery.limit' layout='total, sizes, prev, pager, next, jumper' :total='total'>
      </el-pagination>
    </div>
    
    <!--弹出窗口：编辑角色-->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="30%">
      <el-form :rules="rules" ref="dataForm" :model="temp" label-position="left" label-width="150px" >
        <el-form-item label="角色名" prop="rname" >
          <el-input v-model="temp.rname"></el-input>
        </el-form-item>
        <el-form-item label="角色值" prop="rval" v-if="dialogStatus=='create'">
          <el-input v-model="temp.rval"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="rdesc">
          <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" placeholder="请输入" v-model="temp.rdesc">
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">创建</el-button>
        <el-button v-else type="primary" @click="updateData">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { queryRole, updateRole, deleteRole, addRole } from '@/api/role'
import { parseTime } from '@/utils'
import { confirm, root } from '@/utils/constants'
import debounce from 'lodash/debounce'

export default {
  name: 'RoleManage',
  data() {
    return {
      tableLoading: false,
      tableData: [],
      total: null,
      tableQuery: {
        page: 1,
        limit: 20,
        rname: undefined,
        sort: '+rid'
      },
      dialogFormVisible: false,
      dialogStatus: '',
      temp: {
        // 表格的下标
        idx: undefined,
        rid: undefined,
        rname: undefined,
        rdesc: undefined,
        rval: undefined,
        created: undefined,
        updated: undefined
      },
      textMap: {
        update: '编辑角色',
        create: '新增角色'
      },
      rules: {
        rname: [{ required: true, message: '必填', trigger: 'blur' }],
        rval: [{ required: true, message: '必填', trigger: 'blur' }]
      }
    }
  },

  created() {
    this.fetchData()
  },

  watch: {
    // 延时查询
    'tableQuery.rname': debounce(function() {
      this.fetchData()
    }, 500)
  },

  methods: {
    parseTime,
    hasAdminRole(row) {
      return row && row.rval === root.rval
    },
    // 查询
    fetchData() {
      this.tableLoading = true
      queryRole(this.tableQuery).then(res => {
        this.tableData = res.data.data.items
        this.total = res.data.data.total
        this.tableLoading = false
      }).catch(() => {
        this.tableData = []
        this.total = 0
        this.tableLoading = false
      })
    },
    // 分页
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.listQuery.current = val
      this.fetchData()
    },

    // 更新
    handleUpdate(idx, row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.idx = idx
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => this.$refs['dataForm'].clearValidate())
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) return
        const tempData = Object.assign({}, this.temp)
        updateRole(tempData).then(res => {
          tempData.updated = res.data.updated
          this.tableData.splice(tempData.idx, 1, tempData)
          this.dialogFormVisible = false
          this.$message.success('更新角色信息成功')
        }).catch(() => {
          this.$message.info('角色名已注册')
        })
      })
    },

    // 更新用户的角色
    handleUpdateRolePerms(idx, row) {
      this.$router.push({
        path: '/sys/role_manage/' + row.rid + '/assign_perm'
      })
    },

    // 删除
    handleDelete(idx, row) {
      this.$confirm('您确定要永久删除该角色？', '提示', confirm)
        .then(() => {
          deleteRole(row).then(res => {
            this.tableData.splice(idx, 1)
            --this.tablePage.total
            this.dialogFormVisible = false
            this.$message.success('删除角色成功')
          })
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },

    // 新增
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.resetForm()
      this.$nextTick(() => this.$refs['dataForm'].clearValidate())
    },
    resetForm() {
      this.temp = {
        idx: undefined,
        rid: undefined,
        rname: undefined,
        rdesc: undefined,
        rval: undefined,
        created: undefined,
        updated: undefined
      }
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (!valid) return
        addRole(this.temp).then(res => {
          this.tableData.unshift(res.data.data)
          this.dialogFormVisible = false
          // 后台传回来新增记录的id
          // this.temp.rid = res.data.rid
          // 后台传回来新增记录的时间
          // this.temp.created = res.data.created
          // this.tableData.unshift(Object.assign({}, this.temp))
          ++this.total
          this.dialogFormVisible = false
          this.message.success('添加角色成功')
          this.resetForm()
        })
      })
    }
  }
}
</script>

